package com.thinkverse.audit.controller;

import com.thinkverse.audit.entity.AuditLog;
import com.thinkverse.audit.service.AuditService;
import com.thinkverse.common.result.Result;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 审计日志控制器
 */
@RestController
@RequestMapping("/audit")
@RequiredArgsConstructor
public class AuditController {
    
    private final AuditService auditService;
    
    /**
     * 记录审计日志
     */
    @PostMapping("/log")
    public Result<String> logAudit(@RequestBody AuditLog auditLog) {
        auditService.logAudit(auditLog);
        return Result.success("日志记录成功");
    }
    
    /**
     * 查询审计日志
     */
    @GetMapping("/logs")
    public Result<List<AuditLog>> getAuditLogs(@RequestParam(required = false) Long userId,
                                            @RequestParam(required = false) String operationType,
                                            @RequestParam(required = false) String startTime,
                                            @RequestParam(required = false) String endTime,
                                            @RequestParam(defaultValue = "0") Integer page,
                                            @RequestParam(defaultValue = "10") Integer size) {
        List<AuditLog> logs = auditService.getAuditLogs(userId, operationType, startTime, endTime, page, size);
        return Result.success(logs);
    }
    
    /**
     * 获取操作统计
     */
    @GetMapping("/statistics")
    public Result<Map<String, Object>> getOperationStatistics(@RequestParam(required = false) String startTime,
                                                           @RequestParam(required = false) String endTime) {
        Map<String, Object> statistics = auditService.getOperationStatistics(startTime, endTime);
        return Result.success(statistics);
    }
    
    /**
     * 获取用户行为分析
     */
    @GetMapping("/user-behavior/{userId}")
    public Result<Map<String, Object>> getUserBehaviorAnalysis(@PathVariable Long userId,
                                                             @RequestParam(required = false) String startTime,
                                                             @RequestParam(required = false) String endTime) {
        Map<String, Object> analysis = auditService.getUserBehaviorAnalysis(userId, startTime, endTime);
        return Result.success(analysis);
    }
}
